//
//  Resources.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "Resources.h"

#import "AFNetworking.h"

@implementation Resources

+ (void)getDataWithName:(NSString *)str block:(myBlock)block error:(errorBlock)errors
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        block(responseObject);
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [str hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [str hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);
        
        errors();
    }];

}

@end
